package bible.sunflower.work.yinfeng.socket;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;

public class DatagramServer {
	
	public static void main(String args[])throws Exception 
	{ 
		DatagramSocket ds = new DatagramSocket(null); // 指定Null很重要，否则Java会自动随机选个可用端口来绑定
        ds.setReuseAddress(true); // 绑定之前先设置Reuse
        ds.bind(new InetSocketAddress(1888)); // 然后再绑定
        System.out.println("REUSEADDR is enabled: " + ds.getReuseAddress()); // 返回结果是true，说明才有效
        DatagramPacket dp = new DatagramPacket(new byte[1024], 1024);
        ds.receive(dp);
        System.out.println("RECEIVE: " + new String(dp.getData()));
	}

}
